package com.yupi.springbootinit.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yupi.springbootinit.mapper.ChartMapper;
import com.yupi.springbootinit.model.dto.chart.ChartQueryRequest;
import com.yupi.springbootinit.model.entity.Chart;
import com.yupi.springbootinit.model.vo.ChartVO;
import com.yupi.springbootinit.service.ChartService;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.stream.Collectors;

/**
* @author a
* @description 针对表【chart(图表信息表)】的数据库操作Service实现
* @createDate 2023-12-02 19:22:19
*/
@Service
public class ChartServiceImpl extends ServiceImpl<ChartMapper, Chart>
    implements ChartService{

    /**
     * 实体类型转换为视图
     * @param chart
     * @param request
     * @return
     */
    @Override
    public ChartVO getChartVO(Chart chart, HttpServletRequest request) {
        return ChartVO.objToVo(chart);
    }

    /**
     * 条件构造
     * @param chartQueryRequest
     * @return
     */
    @Override
    public Wrapper<Chart> getQueryWrapper(ChartQueryRequest chartQueryRequest) {
        // 构造sql语句
        // 1. Create a new QueryWrapper instance
        QueryWrapper<Chart> queryWrapper = new QueryWrapper<>();

        // 2. Set the entity class for the query
        queryWrapper.setEntityClass(Chart.class);

        // 3. Set the query conditions based on the request
        if (chartQueryRequest.getChartType() != null) {
            queryWrapper.ge("chart_type", chartQueryRequest.getChartType());
        }
        if (chartQueryRequest.getGoal() != null) {
            queryWrapper.le("goal", chartQueryRequest.getGoal());
        }
        if (StrUtil.isNotEmpty(chartQueryRequest.getName())) {
            queryWrapper.like("`name`", chartQueryRequest.getName());
        }
        if (chartQueryRequest.getUserId()  != null) {
            queryWrapper.eq("user_id", chartQueryRequest.getUserId());
        }

        return queryWrapper;
    }

    @Override
    public Page<ChartVO> getChartVOPage(Page<Chart> chartPage, HttpServletRequest request) {
        List<Chart> chartList = chartPage.getRecords();
        Page<ChartVO> chartVOPage = new Page<>(chartPage.getCurrent(), chartPage.getSize(), chartPage.getTotal());
        if (CollectionUtils.isEmpty(chartList)) {
            return chartVOPage;
        }
        List<ChartVO> chartVOS = chartList.stream().map(item -> {
            ChartVO chartVO = new ChartVO();
            BeanUtils.copyProperties(item, chartVO);
            return chartVO;
        }).collect(Collectors.toList());
        chartVOPage.setRecords(chartVOS);

        return chartVOPage;
    }

    public void saveChartCsv(String chartId, String chartCsv,
                                String chartData) {
        //baseMapper.createTable(chartId, chartCsv, chartData);
    }
}




